package com.visionblue.business.ship.util.location;

import android.content.Context;
import android.util.Log;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;


/**
 * 项目名称：com.visionblue.sea.client
 * 类描述：
 * 创建人：duzepeng
 * 创建时间：2019-12-26
 * 修改人：
 * 修改时间：
 * 修改备注：
 */

public class MapLocationHelper {

    /**
     * 声明mlocationClient对象
     */
    private LocationClient mLocationClient;
    /**
     * 声明mLocationOption对象
     */
//    public LocationClientOption mLocationOption = null;
    private LocationCallBack mLocationCallBack;
    private Context mContext;

    public MapLocationHelper(Context context) {
        mContext = context;
        initLocation();
    }

    public MapLocationHelper(Context context, LocationCallBack locationCallback) {
        mContext = context;
        mLocationCallBack = locationCallback;
        initLocation();
    }

    /**
     * 初始化定位参数
     */
    private void initLocation() {
        mLocationClient = new LocationClient(mContext);
        LocationClientOption option = new LocationClientOption();
        // 设置定位监听
        mLocationClient.registerLocationListener(new BDAbstractLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation location) {

                if (location != null && location.getLocType() != BDLocation.TypeServerError) {
                    if (mLocationCallBack != null) {
                        mLocationCallBack.onCallLocationSuc(location);
                    }
                } else {
                    if (mLocationCallBack != null) {
                        mLocationCallBack.onErr();
                    }
                }
            }
        });
        // 设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        // 设置定位间隔,单位毫秒,默认为2000ms
        option.setScanSpan(60000);
        option.setCoorType("bd09ll");
        option.setOpenGps(true);
        option.setOnceLocation(false);
        // 设置定位参数
        mLocationClient.setLocOption(option);
    }

    /**
     * 设置回调
     *
     * @param locationCallBack
     */
    public void setLocationCallBack(LocationCallBack locationCallBack) {
        mLocationCallBack = locationCallBack;
    }

    /**
     * 开启定位
     */
    public void startMapLocation() {
        if (!mLocationClient.isStarted()) {
            mLocationClient.start();
        }
    }

    /**
     * 停止定位服务
     */
    public void stopMapLocation() {
        if (mLocationClient.isStarted()) {
            mLocationClient.stop();
        }
    }

//    @Override
//    public void onLocationChanged(AMapLocation aMapLocation) {
//        if (aMapLocation != null) {
//            if (aMapLocation.getErrorCode() == 0) {
//                Log.i("MapLocationHelper", "onLocationChanged: " + aMapLocation.getLatitude() + "," + aMapLocation.getLongitude());
//                // 可在其中解析amapLocation获取相应内容。
//                mLocationCallBack.onCallLocationSuc(aMapLocation);
//                stopMapLocation();
//            } else {
//                //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
//                Log.e("AmapError", "location Error, ErrCode:"
//                        + aMapLocation.getErrorCode() + ", errInfo:"
//                        + aMapLocation.getErrorInfo());
//                mLocationCallBack.onErr();
//                stopMapLocation();
//            }
//        } else {
//            mLocationCallBack.onErr();
//            stopMapLocation();
//        }
//    }

}
